import 'package:flutter/material.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/replacement_agreement.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/common/seckill_order_detail_goods_arr.dart';
import 'package:xinxuan/common/seckill_order_detail_info_body.dart';
import 'package:xinxuan/common/seckill_order_detail_info_item.dart';
import 'package:xinxuan/controllers/order_controller/seckill_order_detail_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:common_utils/common_utils.dart';

class SeckillOrderDetailBuyers extends StatelessWidget {

  // final SeckillOrderDetailData seckillOrderDetailData;
  final SeckillOrderDetailPageController controller;
  SeckillOrderDetailBuyers({
    // required this.seckillOrderDetailData,
    required this.controller
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      body: ScrollConfigurationCommon(child: ListView(
        children: [
          _addressInfo(),
          SizedBox(height: 12.h),
          SeckillOrderDetailGoodsArr(seckillOrderDetailData: controller.seckillOrderDetailData.value),
          SizedBox(height: 12.h),
          SeckillOrderDetailInfoBody(
            title: '订单信息', 
            children: [
              SeckillOrderDetailInfoItem(title: '订单编号', value: '${controller.seckillOrderDetailData.value.orderSn}'),
              SeckillOrderDetailInfoItem(title: '创建时间', value: '${controller.seckillOrderDetailData.value.createdAt}'),
            ]
          ),
          SizedBox(height: 12.h),
          SeckillOrderDetailInfoBody(
            title: '卖家信息', 
            children: [
              SeckillOrderDetailInfoItem(title: '姓名', value: '${controller.seckillOrderDetailData.value.seller!.accountName}', showCopy: true),
              SeckillOrderDetailInfoItem(title: '银行卡账号', value: '${controller.seckillOrderDetailData.value.seller!.cardNumber}', showCopy: true),
              SeckillOrderDetailInfoItem(title: '开户行', value: '${controller.seckillOrderDetailData.value.seller!.bank}', showCopy: true),
              SeckillOrderDetailInfoItem(title: '联系电话', value: '${controller.seckillOrderDetailData.value.seller!.mobile}', showCopy: true)
            ]
          ),
          controller.seckillOrderDetailData.value.status == 1 ? Obx(() => Column(
            children: [
              SizedBox(height: 25.h),
              Container(
                margin: EdgeInsets.only(top: 16.h),
                alignment: Alignment.center,
                child: Column(
                  children: [
                    Text.rich(
                      TextSpan(
                        text: '请在',
                        style: TextStyle(
                          color: textBlackColor,
                          fontSize: 14.sp,
                          fontWeight: FontWeight.bold
                        ),
                        children: [
                          TextSpan(
                            text: '${DateUtil.formatDateMs(controller.duration.value * 1000, format: 'mm:ss' )}分钟内',
                            style: TextStyle(
                              color: mainTextColor,
                              fontSize: 14.sp,
                              fontWeight: FontWeight.bold
                            ),
                          ),
                          TextSpan(
                            text: '完成支付，',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp,
                              fontWeight: FontWeight.bold
                            ),
                          )
                        ]
                      )
                    ),
                    SizedBox(height: 8.h),
                    Text(
                      '否则订单将会自动取消',
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.bold
                      ),
                    )
                  ],
                ),
              )
            ],
          )) : Container(),
          SizedBox(height: 25.h)
        ],
      )),
      bottomNavigationBar: BottomAppBar(
        elevation: 0,
        child: Obx(() => Container(
          color: bgWhiteColor,
          height: controller.seckillOrderDetailData.value.status == 3 || controller.seckillOrderDetailData.value.status == 4 ? 56.h : 93.h,
          child: Column(
            children: [
              controller.seckillOrderDetailData.value.status == 3 || controller.seckillOrderDetailData.value.status == 4 ? Container() : ReplacementAgreement(
                consentAgreement: controller.consentAgreement.value, 
                onTap: () {
                  controller.consentAgreement.value = !controller.consentAgreement.value;
                }
              ),
              Container(
                height: 56.h,
                color: bgWhiteColor,
                padding: EdgeInsets.symmetric(horizontal: 12.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text.rich(
                      TextSpan(
                        text: '总价：',
                        style: TextStyle(
                          color: textBlackColor
                        ),
                        children: [
                          TextSpan(
                            text: '¥',
                            style: TextStyle(
                              color: mainTextColor,
                              fontSize: 18.sp,
                              fontWeight: FontWeight.bold
                            )
                          ),
                          TextSpan(
                            text: '${controller.seckillOrderDetailData.value.totalAmount}',
                            style: TextStyle(
                              color: mainTextColor,
                              fontSize: 22.sp,
                              fontWeight: FontWeight.bold
                            )
                          )
                        ]
                      )
                    ),
                    _getStatusBtn(controller.seckillOrderDetailData.value.status!, controller.seckillOrderDetailData.value.id!)
                  ],
                ),
              )
            ],
          ),
        )),
      ),
    );
  }

  /// 地址信息
  Widget _addressInfo() {
    return Stack(
      children: [
        Container(
          height: 80.h,
          width: 375.w,
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.centerRight,
              end: Alignment.centerLeft,
              colors: [
                bgMainColor,
                bgExcessiveColor
              ]
            )
          )
        ),
        Positioned(
          top: 0,
          right: 0,
          child: Image.asset(
            'assets/icons/icon_address_bg.png',
            width: 133.w,
            height: 80.h,
            fit: BoxFit.cover,
          )
        ),
        Positioned(
          top: 0,
          left: 0,
          child: Container(
            height: 80.h,
            width: 375.w,
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Row(
              children: [
                Image.asset(
                  'assets/icons/icon_address.png',
                  width: 30.w,
                  height: 30.w,
                  fit: BoxFit.cover,
                ),
                SizedBox(width: 12.w),
                Expanded(child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '${controller.seckillOrderDetailData.value.buyer!.accountName}',
                      style: TextStyle(
                        color: textWhiteColor,
                        fontSize: 14.sp
                      ),
                    ),
                    SizedBox(height: 6.h),
                    Text(
                      '${controller.seckillOrderDetailData.value.buyer!.mobile}',
                      overflow: TextOverflow.ellipsis,
                      maxLines: 2,
                      style: TextStyle(
                        color: textWhiteColor,
                        fontSize: 14.sp
                      ),
                    )
                  ],
                ))
              ],
            ),
          )
        )
      ],
    );
  }
  
  /// 根据状态获取按钮
  /// status 1 => 待付款 2 => 待收款 3 => 已售出 4 => 已取消
  Widget _getStatusBtn(int status, int orderId) {
    List<Widget> children = [];
    if (status == 1) {
      children.add(Container(
        width: 120.w,
        child: ButtonUtil(
          text: '我已确认付款', 
          fontSize: 14.sp, 
          borderRadius: 18.w, 
          height: 36.h, 
          onTop: () {
            controller.seckillOrderConfirmPayment();
          }
        )
      ));
    } else if (status == 2) {
      children.add(Opacity(
        opacity: .5,
        child: Container(
          width: 120.w,
          child: ButtonUtil(
            text: '我已确认付款', 
            fontSize: 14.sp, 
            borderRadius: 18.w, 
            height: 36.h, 
            onTop: () {}
          )
        ),
      ));
    }
    return Row(
      children: children,
    );
  }

}